package demo.IOInputOutput;

import jdk.jfr.events.FileReadEvent;

import java.io.*;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/15-08-15-9:34
 * @Description：demo.IOStream    缓冲流
 */
/*
字节缓冲流
    BufferedInputStream
    BufferedOutputStream
字符缓冲流
    BufferedReader
        可以读取一行数据readLine()   ,结束标志为 判断是否为空
    BufferedWriter
        写换行分隔符\r\n
 */
public class IO_Buffer {
    public static void main(String[] args) {
        //声明流
        BufferedWriter bw = null;
        BufferedReader br = null;
        InputStreamReader bi = null;
        try {
            //创建流
            bw = new BufferedWriter(new FileWriter("a.txt"));
           // br = new BufferedReader(new FileReader("a.txt"));
            //如果是字节流,需要先把字节流转换为字符流     inputStream  ->  InputStreamReader
            bi = new InputStreamReader(new FileInputStream("a.txt"));

            br = new BufferedReader(bi);
            //使用流
            bw.write("edhuiehweuygfieh");   //字符串末尾加\r\n   换行
            bw.newLine();    //自动换行
            bw.write("hello,中国");
            bw.flush();        //一定要刷新，否则读不出数据
            String line = null;
            while((line = br.readLine())!= null){
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭流
            try {
                if(bw != null) {
                    bw.close();
                }
                if(br != null){
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
